package com.cloudDemoProduct.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudDemoProduct.service.ProductService;
import entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Daft_V
 * @Date 2022/5/5 23:48
 */
@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    /**
     * 查询所有product列表
     *
     * @return JSONObject
     */
    @GetMapping("/productList")
    @ResponseBody
    public JSONObject productList() {
        List<Product> productList = productService.productList();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", productList);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }

    /**
     * 根据id查询product
     *
     * @param id 主键
     * @return Product
     */
    @GetMapping("/queryProductById")
    @ResponseBody
    public Product queryProductById(int id) {
        Product product = productService.queryProductById(id);
        return product;
    }

    /**
     * 更新库存数量
     *
     * @param pid    商品id
     * @param number 购买数量
     * @return Boolean
     */
    @PostMapping("/updateStock")
    @ResponseBody
    public Boolean updateStock(int pid, int number) {
        return productService.updateStock(pid, number);
    }
}
